$('.reply-message').focus(function(){
	if($('#used').text()==0){
		alert("权限不足。验证邮箱并完善你的个人信息之后方可开启留言评论功能。");	
		$('.reply-message').attr('disabled', '');
		return false;
	}
})

$('.reply-message').keyup(function(){
	if($(this).val()!=''){
		$(this).parent().find('.button').removeAttr('disabled');
	}
	else{
		$(this).parent().find('.button').attr('disabled', '');
	}
})

$('#message').keyup(function(){
	if($('#message').val()!='' && $('#message').val().length>4){
		$('#send-message-btn').removeAttr('disabled');
	}
	else{
		$('#send-message-btn').attr('disabled', '');
	}
})

$('.side-bar-tag').click(function(){
	var acti = $(this).attr('t');
	if(acti==1){
		$(this).attr('t', 0);
		$(this).css('opacity', '0.5'); $(this).css('filter', 'alpha(opacity=50)'); 
		}
	else{
		$(this).attr('t', 1);
		$(this).css('opacity', '1'); $(this).css('filter', 'alpha(opacity=100)'); 
		}
	tags = '';
	var len = $('.side-bar-tag').length;

	for(var i=0;i<len;i=i+1){
		var t = $('.side-bar-tag').get(i).getAttribute('t');
		var pos = $('.side-bar-tag').get(i).getAttribute('pos');
		tags = tags + t;
	}
	var url = window.location.href;
	var str = url.substr(url.indexOf('/g/')+3);
	var strs=str.split("/");
	if(strs[2]) var strpage='/'+strs[2];
	else var strpage='';
	window.location.assign(base_url_feeds+'feeds/g/'+$('#zone-id').text()+'/'+tags+strpage);
});


$('.side-bar-tag-zone').click(function(){
	var acti = $(this).attr('t');
	if(acti==1){
		$(this).attr('t', 0);
		$(this).css('opacity', '0.5'); $(this).css('filter', 'alpha(opacity=50)'); 
		}
	else{
		$(this).attr('t', 1);
		$(this).css('opacity', '1'); $(this).css('filter', 'alpha(opacity=100)'); 
		}
	tags = '';
	var len = $('.side-bar-tag-zone').length;

	for(var i=0;i<len;i=i+1){
		var t = $('.side-bar-tag-zone').get(i).getAttribute('t');
		var pos = $('.side-bar-tag-zone').get(i).getAttribute('pos');
		tags = tags + t;
	}
	var zone_id = $('#zone-id').text();
	if(zone_id == "" || zone_id == null)
	{
		zone_id = 1;
	}
	var url = window.location.href;
	if(url.indexOf('/g/')!=-1){
		var str = url.substr(url.indexOf('/g/')+3);
		var strs=str.split("/");
		if(strs[2]) var strpage='/'+strs[2];
		else var strpage='';
	}else var strpage='';
	window.location.assign(base_url_feeds+'zone/g/'+zone_id+'/'+tags+strpage);
});

$('.new-feed-tags').click(function(){
	$('#send-message-success').hide();
	var acti = $(this).attr('t');
	if(acti==1){
		$(this).attr('t', 0);
		$(this).css('opacity', '0.5'); $(this).css('filter', 'alpha(opacity=50)'); 
		}
	else{
		$(this).attr('t', 1);
		$(this).css('opacity', '1'); $(this).css('filter', 'alpha(opacity=100)'); 
		}
})

$('.imginfo-delete-btn').click(function(){
	$(this).parent().find('a').hide();
	$(this).parent().find('a').removeClass('fancybox');
	$(this).parent().find('form').show();
	$(this).parent().find('span').hide();
	var deleted_id = $(this).parent().find('form').find('input').get(1).value;
	$.ajax({
		type: 'POST',
		url: base_url_feeds+'feeds/image_delete', 
		data: {
				deleted_id: deleted_id
			},
		dataType: 'text',  
		cache: false
		});
});
	
$('.upload-entry').change(function(){
	$('#send-message-success').hide();
	var ext = $(this).val().split('.').pop().toLowerCase();
	if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
			alert('图片格式不正确。只接受.gif, .png, .jpe, .jpeg格式的图片。');
			return false;
	}
	$(this).parent().find('img').attr('src',base_url+'assets/img/upload.png');
	$(this).parent().parent().find('.imginfo-upload-btn').click();
	$('.upload-entry').val('');
});


$('#imginfo-form-1').ajaxForm({
		beforeSend: function() {
      $('#imginfo-form-1').find('img').attr('src',base_url+'assets/img/uploading.gif');
			$('#imginfo-form-1').find('.upload-entry').attr('disabled', '');
			$('#imginfo-form-1').find('.imginfo-upload-btn').attr('disabled','');
			$('#imginfo-form-1').find('.imginfo-upload-btn').val('上传中...');
    },
    success: function(data) {
			if(data=='error'||data==''){
				$('#imginfo-form-1').find('.imginfo-upload-btn').removeAttr('disabled');
				$('#imginfo-form-1').find('.imginfo-upload-btn').val('上传');
				$('#imginfo-form-1').find('.imginfo-upload-btn').css('visibility','hidden');
				$('#imginfo-form-1').find('.upload-entry').removeAttr('disabled');
				$('#imginfo-form-1').find('img').attr('src', base_url+'assets/img/add.png'); 
				$('#imginfo-form-1').hide();
				$('#imginfo-form-1').parent().find('form').show();
				alert('照片上传不成功，照片大小超过2MB上限。请重新选择较小图片');
				return;	
			}
			var img = data.split(" ")[0];
			var img_thumb = data.split(" ")[1];
			$('#imginfo-form-1').find('.imginfo-upload-btn').removeAttr('disabled');
			$('#imginfo-form-1').find('.imginfo-upload-btn').val('上传');
			$('#imginfo-form-1').find('.imginfo-upload-btn').css('visibility','hidden');
			$('#imginfo-form-1').find('.upload-entry').removeAttr('disabled');
			$('#imginfo-form-1').find('img').attr('src', base_url+'assets/img/add.png');  
			$('#imginfo-form-1').parent().find('a').find('img').attr('src',img_thumb);
			$('#imginfo-form-1').parent().find('a').addClass('fancybox');
			$('#imginfo-form-1').parent().find('a').attr('href', img);
			$('#imginfo-form-1').parent().find('a').show();
			$('#imginfo-form-1').hide();
			$('#imginfo-form-1').parent().find('span').css('display','block');
    },
		complete: function(data) {
						 
		}
}); 

$('#imginfo-form-2').ajaxForm({
		beforeSend: function() {
      $('#imginfo-form-2').find('img').attr('src',base_url+'assets/img/uploading.gif');
			$('#imginfo-form-2').find('.upload-entry').attr('disabled', '');
			$('#imginfo-form-2').find('.imginfo-upload-btn').attr('disabled','');
			$('#imginfo-form-2').find('.imginfo-upload-btn').val('上传中...');
    },
    success: function(data) {
			if(data=='error'||data==''){
				$('#imginfo-form-2').find('.imginfo-upload-btn').removeAttr('disabled');
				$('#imginfo-form-2').find('.imginfo-upload-btn').val('上传');
				$('#imginfo-form-2').find('.imginfo-upload-btn').css('visibility','hidden');
				$('#imginfo-form-2').find('.upload-entry').removeAttr('disabled');
				$('#imginfo-form-2').find('img').attr('src', base_url+'assets/img/add.png'); 
				$('#imginfo-form-2').hide();
				$('#imginfo-form-2').parent().find('form').show();
				alert('照片上传不成功，照片大小超过2MB上限。请重新选择较小图片');
				return;	
			}
			var img = data.split(" ")[0];
			var img_thumb = data.split(" ")[1];
			$('#imginfo-form-2').find('.imginfo-upload-btn').removeAttr('disabled');
			$('#imginfo-form-2').find('.imginfo-upload-btn').val('上传');
			$('#imginfo-form-2').find('.imginfo-upload-btn').css('visibility','hidden');
			$('#imginfo-form-2').find('.upload-entry').removeAttr('disabled');
			$('#imginfo-form-2').find('img').attr('src', base_url+'assets/img/add.png');  
			$('#imginfo-form-2').parent().find('a').find('img').attr('src',img_thumb);
			$('#imginfo-form-2').parent().find('a').addClass('fancybox');
			$('#imginfo-form-2').parent().find('a').attr('href', img);
			$('#imginfo-form-2').parent().find('a').show();
			$('#imginfo-form-2').hide();
			$('#imginfo-form-2').parent().find('span').css('display','block');
    },
		complete: function(data) {
						 
		}
}); 

$('#imginfo-form-3').ajaxForm({
		beforeSend: function() {
      $('#imginfo-form-3').find('img').attr('src',base_url+'assets/img/uploading.gif');
			$('#imginfo-form-3').find('.upload-entry').attr('disabled', '');
			$('#imginfo-form-3').find('.imginfo-upload-btn').attr('disabled','');
			$('#imginfo-form-3').find('.imginfo-upload-btn').val('上传中...');
    },
    success: function(data) {
			if(data=='error'||data==''){
				$('#imginfo-form-3').find('.imginfo-upload-btn').removeAttr('disabled');
				$('#imginfo-form-3').find('.imginfo-upload-btn').val('上传');
				$('#imginfo-form-3').find('.imginfo-upload-btn').css('visibility','hidden');
				$('#imginfo-form-3').find('.upload-entry').removeAttr('disabled');
				$('#imginfo-form-3').find('img').attr('src', base_url+'assets/img/add.png'); 
				$('#imginfo-form-3').hide();
				$('#imginfo-form-3').parent().find('form').show();
				alert('照片上传不成功，照片大小超过2MB上限。请重新选择较小图片');
				return;	
			}
			var img = data.split(" ")[0];
			var img_thumb = data.split(" ")[1];
			$('#imginfo-form-3').find('.imginfo-upload-btn').removeAttr('disabled');
			$('#imginfo-form-3').find('.imginfo-upload-btn').val('上传');
			$('#imginfo-form-3').find('.imginfo-upload-btn').css('visibility','hidden');
			$('#imginfo-form-3').find('.upload-entry').removeAttr('disabled');
			$('#imginfo-form-3').find('img').attr('src', base_url+'assets/img/add.png');  
			$('#imginfo-form-3').parent().find('a').find('img').attr('src',img_thumb);
			$('#imginfo-form-3').parent().find('a').addClass('fancybox');
			$('#imginfo-form-3').parent().find('a').attr('href', img);
			$('#imginfo-form-3').parent().find('a').show();
			$('#imginfo-form-3').hide();
			$('#imginfo-form-3').parent().find('span').css('display','block');
    },
		complete: function(data) {
						 
		}
}); 

$('.post-feed').focus(function(){
	if($('#used').text()==0){
		alert("权限不足。验证邮箱并完善你的个人信息之后方可开启留言评论功能。");	
		$('.post-feed').attr('disabled', '');
		return false;
	}
	$('#send-message-success').hide();
	$(this).parent().find('.feed-options').show('fast');
})

$('.load-more-reply-btn').click(function(){
	thread_id = $(this).parent().find('.thread-id').text();	
  current_user_id = $(this).parent().find('.current-user-id').text();
	$.ajax({
		type: 'POST',
		url: base_url_feeds+'feeds/load_more_reply', 
		data: {
			thread_id: thread_id
		}, 
		success: function(data){
			allobj = JSON.parse(data);
			$('#feed-reply-holder-'+thread_id).empty();
			for(i=1;i<allobj.length;i=i+1){
				obj = allobj[i];
				reply ='<div class="row collpase feed-reply"><div><div>'+
								'<a href="'+base_url_user+'profile/u/'+obj.sender_id+'"><img class="left" src="'+obj.sender_image+'"></a>'+
								'<div class="name">'+
									'<span class="message">'+
										'<a href="'+base_url_user+'profile/u/'+obj.sender_id+'"><b>'+obj.sender_name+'</b></a>&nbsp;';
				if(obj.private==1){
					reply = reply + '<img title="悄悄话" src="'+base_url+'assets/img/private.png">';	
				}
				reply = reply + '回复 '+
										obj.target_name+'：'+obj.message+'</span>'+
									'<span>';
				if(current_user_id!=0&&obj.sender_id!=current_user_id){
					reply = reply + ' <a class="reply-btn">回复</a>';	
				}
				if(obj.sender_id==current_user_id||$('#thread-owner-id-'+thread_id).text()==current_user_id){
					reply = reply + ' <a class="delete-message-btn">删除</a>';
				}
				reply = reply +
										'<div class="hidden thread-id">'+obj.thread_id+'</div>'+
										'<div class="hidden sender-name">'+obj.sender_name+'</div>'+
										'<div class="hidden sender-id">'+obj.sender_id+'</div>'+
										'<div class="hidden notice-id">'+obj.id+'</div>'+						
									'</span>'+
								'</div>'+
							'</div></div></div>';
				$('#feed-reply-holder-'+thread_id).append(reply);
			}
			$('#endjs').empty();
			$('#endjs').append('<script type="text/javascript" src="'+base_url_feeds+'assets/js/replydelete.js"></script>');
		},
		dataType: 'text',  
		cache: false
	});	
})

$('#send-message-btn').click(function(){
	if($('#used').text()==0){
		alert("权限不足。验证邮箱并完善你的个人信息之后方可开启留言评论功能。");	
		return false;
	}
	if($(this).attr('disabled')!=null){return false;}  
	if($('#message').val()!='' && $('#message').val().length>4 ){
		$('#send-message-success').hide();
		$('#send-message-loading').css('display','inline-block');
		message = $('#message').val();
		tags = '';
		var len = $('.new-feed-tags').length;
		for(var i=0;i<len;i=i+1){
			var t = $('.new-feed-tags').get(i).getAttribute('t');
			tags = tags + t;	
		}
		feed_img_1 = $('#feed-img-1').attr('href');
		feed_img_2 = $('#feed-img-2').attr('href');
		feed_img_3 = $('#feed-img-3').attr('href');
		feed_img_thumb_1 = $('#feed-img-thumb-1').attr('src');
		feed_img_thumb_2 = $('#feed-img-thumb-2').attr('src');
		feed_img_thumb_3 = $('#feed-img-thumb-3').attr('src');
		$.ajax({
			type: 'POST',
			url: base_url_feeds+'feeds/message', 
			data: {
				zone_id: $('#zone-id').text(),	
				message: message,
				tags: tags,
				feed_img_1: feed_img_1,
				feed_img_2: feed_img_2,
				feed_img_3: feed_img_3,
				feed_img_thumb_1: feed_img_thumb_1,
				feed_img_thumb_2: feed_img_thumb_2,
				feed_img_thumb_3: feed_img_thumb_3
			},
			success: function(data){
				obj = JSON.parse(data);
				$('#send-message-loading').hide();
				if(obj.sendmessageerror){
					$('#popup').css('width', '280px');
					$('#popup').text(obj.sendmessageerror);
					$('#popup').stop(true, true).fadeOut();
					$('#popup').show();
					$('#popup').fadeOut(4000);
					return;
				}
				$('#message').val('');
				$('#send-message-btn').attr('disabled', '');
				$('.imginfo-delete-btn').parent().find('a').hide();
				$('.imginfo-delete-btn').parent().find('a').removeClass('fancybox');
				$('.imginfo-delete-btn').parent().find('form').show();
				$('.imginfo-delete-btn').parent().find('span').hide();
				$('.new-feed-tags').attr('t', 0);
				$('.new-feed-tags').css('opacity', '0.5'); $('.new-feed-tags').css('filter', 'alpha(opacity=50)');
				$('#send-message-success').css('display', 'inline-block');	
				$('#feed-img-1').attr('href', '');
				$('#feed-img-2').attr('href', '');
				$('#feed-img-3').attr('href', '');
				$('#feed-img-thumb-1').attr('src', '');
				$('#feed-img-thumb-2').attr('src', '');
				$('#feed-img-thumb-3').attr('src', '');
				showtags = false;
				for(i=0;i<obj.tags.length;i=i+1){
					if(obj.tags[i][1]){
						showtags = true;	
					}
				}
				showimg = obj.feed_img_1!=''||obj.feed_img_2!=''||obj.feed_img_3!=''?true:false;
				reply = '<div class="start-loc"></div><div class="large-12 columns feed"><hr><div class="anchor" id="thread_'+obj.thread_id+'"></div>'+
								'<div class="content panel">'+
									'<div class="img-wrapper">'+
										'<a href="'+base_url_user+'profile/u/'+obj.sender_id+'"><img class="radius left" src="'+obj.sender_image+'"></a>'+
									'</div>'+
									'<div class="message-form">'+
										'<div class="name">'+
											'<span>'+
												'<a href="'+base_url_user+'profile/u/'+obj.sender_id+'">'+
												'<b>'+obj.sender_name+'</b></a> ('+obj.time+')'+
											'</span>'+
										'</div>'+
										'<div class="message-holder">'+
											'<span class="message">';
				if(obj.private==1){
					reply = reply + '<img title="悄悄话" src="'+base_url+'assets/img/private.png">';	
				}
				reply = reply +	obj.message +' </span>'+ 
											'<span>'+
												'<a class="delete-message-btn">删除</a>'+
												'<div class="hidden thread-id">'+obj.thread_id+'</div>'+
												'<div class="hidden notice-id">'+obj.id+'</div>'+
											'</span>'+
											'<div class="feed-options">';
				if(showtags){
					reply = reply + '<div class="tags">';
					for(i=0;i<obj.tags.length;i=i+1){
						if(obj.tags[i][1]){
							reply = reply + '<span class="label radius solid">'+obj.tags[i][0]+'</span> ';
						}
					}
					reply = reply + '</div>';
				}		
				if(showimg){
					reply = reply + '<div class="feed-img-wrapper">';
					if(obj.feed_img_1!=''){
						reply = reply + '<div class="feed-img">'+
															'<a class="fancybox" rel="gallery'+obj.thread_id+'" href="'+obj.feed_img_1+'">'+
																'<img class="th feed-image" src="'+obj.feed_img_thumb_1+'">'+
															'</a>'+
														'</div> ';	
					}
					if(obj.feed_img_2!=''){
						reply = reply + '<div class="feed-img">'+
															'<a class="fancybox" rel="gallery'+obj.thread_id+'" href="'+obj.feed_img_2+'">'+
																'<img class="th feed-image" src="'+obj.feed_img_thumb_2+'">'+
															'</a>'+
														'</div> ';	
					}
					if(obj.feed_img_3!=''){
						reply = reply + '<div class="feed-img">'+
															'<a class="fancybox" rel="gallery'+obj.thread_id+'" href="'+obj.feed_img_3+'">'+
																'<img class="th feed-image" src="'+obj.feed_img_thumb_3+'">'+
															'</a>'+
														'</div>';	

					}
					reply = reply + '</div>';	
				}					
				reply = reply + '</div>'+
									 '</div><div class="feed-reply-holder"></div>'+
									'</div>'+
								'</div>'+
							'</div>';
				$('.start-loc').before(reply);
				$('.start-loc').last().remove();
				$('#endjs').empty();
				$('#endjs').append('<script type="text/javascript" src="'+base_url_feeds+'assets/js/replydelete.js"></script>');

				popupwindow("+"+1+"人品");
				$('#rp-record').text(parseInt($('#rp-record').text())+parseInt(1));
			}, 
			dataType: 'text',  
			cache: false
			});
	}
})
